package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.KnowledgeBase;
import com.mxpio.erp.common.equipment.service.KnowledgeBaseService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "KnowledgeBaseController", description = "知识库")
@RestController
@RequestMapping("/erp/equipment/knowledge/base")
public class KnowledgeBaseController {

    @Autowired
    private KnowledgeBaseService service;

    @GetMapping("list")
    @Operation(summary = "知识库列表", description = "获取知识库列表（无分页）", method = "GET")
    public Result<List<KnowledgeBase>> list(@Parameter(description="查询参数") Criteria criteria){
        criteria.addCriterion("status", Operator.EQ,"1");
        List<KnowledgeBase> list = service.list(KnowledgeBase.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "知识库列表", description = "获取知识库列表（分页）", method = "GET")
    public Result<Page<KnowledgeBase>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        criteria.addCriterion("status", Operator.EQ,"1");
        Page<KnowledgeBase> pageResult = service.listPage(KnowledgeBase.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("auditList")
    @Operation(summary = "知识库待审批列表", description = "获取知识库待审批列表（无分页）", method = "GET")
    public Result<List<KnowledgeBase>> auditList(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        criteria.addCriterion("status", Operator.EQ,"2");
        List<KnowledgeBase> list = service.list(KnowledgeBase.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("auditPage")
    @Operation(summary = "知识库待审批列表", description = "获取知识库待审批列表（分页）", method = "GET")
    public Result<Page<KnowledgeBase>> auditPage(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        criteria.addCriterion("status", Operator.EQ,"2");
        Page<KnowledgeBase> pageResult = service.listPage(KnowledgeBase.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取知识库列表", description = "根据ID获取知识库列表", method = "GET")
    public Result<KnowledgeBase> getById(@PathVariable(name = "id", required = true) String id) {
        KnowledgeBase knowledgeBase = service.getById(KnowledgeBase.class, id);
        return Result.OK(knowledgeBase);
    }

    @RequestMapping("add")
    @Operation(summary = "保存知识库", description = "保存知识库")
    public Result<KnowledgeBase> add(@RequestBody KnowledgeBase knowledgeBase) {
        service.save(knowledgeBase);
        return Result.OK(knowledgeBase);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑知识库", description = "编辑知识库", method = "PUT")
    public Result<KnowledgeBase> edit(@RequestBody KnowledgeBase knowledgeBase) {
        service.update(knowledgeBase);
        return Result.OK(knowledgeBase);
    }

    @DeleteMapping("remove/{familys}")
    @Operation(summary = "根据family删除知识库", description = "删除知识库,多个family用英文逗号分隔", method = "DELETE")
    public Result<?> remove(@PathVariable(name = "familys", required = true) String familys) {
        return service.removeByFamilys(familys);
        //return Result.error("知识库不允许删除");
        /*String[] id = ids.split(",");
        for(String key : id){
            service.delete(KnowledgeBase.class, key);
        }
        return Result.OK();*/
    }

    @PutMapping("upgrade")
    @Operation(summary = "申请审批", description = "申请审批", method = "PUT")
    public Result<KnowledgeBase> upgrade(@RequestBody KnowledgeBase knowledgeBase) {
        if(StringUtils.isBlank(knowledgeBase.getPrevId())){
            return Result.error("前一版本ID不能为空");
        }
        return service.upgrade(knowledgeBase);
    }

    @GetMapping("accept/{id}")
    @Operation(summary = "审批通过", description = "审批通过", method = "GET")
    public Result<KnowledgeBase> accept(@PathVariable(name = "id", required = true) String id) {
        return service.accept(id);
    }

    @GetMapping("reject/{id}")
    @Operation(summary = "审批驳回", description = "审批驳回", method = "GET")
    public Result<KnowledgeBase> reject(@PathVariable(name = "id", required = true) String id,@Parameter(description="审批意见")String auditView) {
        if(StringUtils.isBlank(auditView)){
            return Result.error("必须填写审批意见");
        }
        return service.reject(id,auditView);
    }

    @GetMapping("history/{id}")
    @Operation(summary = "知识库历史列表", description = "获取知识库历史列表（无分页）", method = "GET")
    public Result<List<KnowledgeBase>> history(@PathVariable(name = "id", required = true) String id){
        return service.history(id);
    }

    @GetMapping("historyPage/{id}")
    @Operation(summary = "知识库历史列表", description = "获取知识库历史列表（有分页）", method = "GET")
    public Result<Page<KnowledgeBase>> historyPage(@PathVariable(name = "id", required = true) String id,@Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo){
        return service.historyPage(id,pageSize,pageNo);
    }
}
